package com.quiz;

import java.math.BigDecimal;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.paypal.android.MEP.CheckoutButton;
import com.paypal.android.MEP.PayPal;
import com.paypal.android.MEP.PayPalPayment;

public class HelpActivity extends Activity{

	private CheckoutButton launchPayPalButton;
	
	private static final int REQUEST_PAYPAL_CHECKOUT = 2;
	private ProgressDialog _progressDialog;
	private boolean _paypalLibraryInit = false;
	private boolean _progressDialogRunning = false;
	private boolean mPurchase = false;
	RelativeLayout relativelayout;
	PayPal pp;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.help_screen);
		TextView textContent = (TextView) findViewById(R.id.help_content);
		Resources res = getResources();
		textContent.setText(res.getString(R.string.help_content_chuc_nang)
				+ res.getString(R.string.help_content_bo_de)
				+ res.getString(R.string.help_content_test)
				+ res.getString(R.string.help_content_cap_nhat_bo_de)
				+ res.getString(R.string.help_content_cau_hinh));

		relativelayout = (RelativeLayout) findViewById(R.id.help_relateve_paypal);
		// PayPal
		if (_paypalLibraryInit) {
			Log.i("if", "dc goi");
			showPayPalButton();
		} else {
			Log.i("else", "dc goi");
			initLibrary();
			checkForPayPalLibraryInit();
			Toast.makeText(HelpActivity.this, "Paypal ready!",
					Toast.LENGTH_SHORT).show();
		}
		launchPayPalButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.i("onClick", "duoc goi");
				PayPalButtonClick(v);
			}
		});
	}// ******************************************************************

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		this.getParent().onBackPressed();
	}

	public void PayPalButtonClick(View v) {
		PayPalPayment newPayment = new PayPalPayment();
		newPayment.setSubtotal(new BigDecimal(1));
		newPayment.setCurrencyType("USD");
		newPayment.setRecipient("nthoa97@student.ctu.edu.vn");
		newPayment.setMerchantName("ThucTap2012");
		Intent checkoutIntent = PayPal.getInstance().checkout(newPayment, this);
		// Use the android's startActivityForResult() and pass in our
		// Intent.
		// This will start the library.
		if (mPurchase) {
			Toast.makeText(HelpActivity.this, "You have already purchased this item!",
					Toast.LENGTH_SHORT).show();
		} else {
			this.startActivityForResult(checkoutIntent,0);
		}
		// Intent paypalIntent = PayPal.getInstance().checkout(newPayment,
		// this);
		// this.startActivityForResult(paypalIntent, 1);
		
	}

	public void showPayPalButton() {
		PayPal pp = PayPal.getInstance();
		launchPayPalButton = pp.getCheckoutButton(HelpActivity.this,
				PayPal.BUTTON_194x37, CheckoutButton.TEXT_PAY);
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		params.bottomMargin = 5;
		params.leftMargin = 50;
		launchPayPalButton.setLayoutParams(params);
		launchPayPalButton.setId(2012);
		// launchPayPalButton.setOnClickListener(HelpActivity.this);
		relativelayout.addView(launchPayPalButton);
	}

	private void removePayPalButton() {
		// Avoid an exception for setting a parent more than once
		if (launchPayPalButton != null) {
			relativelayout.removeView(launchPayPalButton);
		}
	}

	public void initLibrary() {
		pp = PayPal.getInstance();
		if (pp == null) {
			pp = PayPal.initWithAppID(this, "APP-80W284485P519543T",
					PayPal.ENV_SANDBOX);
			pp.setLanguage("en_US");
			pp.setFeesPayer(PayPal.FEEPAYER_EACHRECEIVER);
			pp.setShippingEnabled(true);
			pp.setDynamicAmountCalculationEnabled(false);
			_paypalLibraryInit = true;
			// if (_progressDialog.isShowing()){
			// _progressDialog.dismiss();
			// _progressDialogRunning = false;
			// }
			Log.i("pp", "created");
		}
	}

	private void checkForPayPalLibraryInit() {
		while (_paypalLibraryInit == false) {
			try {

				Thread.sleep(500);
			} catch (InterruptedException e) {

				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				builder.setMessage("Error initializing PayPal Library")
						.setCancelable(false)
						.setPositiveButton("OK",
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											int id) {
										// Could do anything here to handle the
										// error
									}
								});
				AlertDialog alert = builder.create();
				alert.show();
			}
		}
		showPayPalButton();
	}
	// This lets us run the initLibrary function
	final Runnable initLibraryRunnable = new Runnable() {
		@Override
		public void run() {
			initLibrary();
		}
	};

}